1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module soup.SSocket;
26 
27 private import gio.Cancellable;
28 private import gio.IOStream;
29 private import gio.InitableIF;
30 private import gio.InitableT;
31 private import glib.ConstructionException;
32 private import glib.ErrorG;
33 private import glib.GException;
34 private import glib.Str;
35 private import gobject.ObjectG;
36 private import gobject.Signals;
37 private import soup.Address;
38 private import soup.c.functions;
39 public  import soup.c.types;
40 private import std.algorithm;
41 
42 
43 /** */
44 public class SSocket : ObjectG, InitableIF
45 {
46 	/** the main Gtk struct */
47 	protected SoupSocket* soupSocket;
48 
49 	/** Get the main Gtk struct */
50 	public SoupSocket* getSSocketStruct(bool transferOwnership = false)
51 	{
52 		if (transferOwnership)
53 			ownedRef = false;
54 		return soupSocket;
55 	}
56 
57 	/** the main Gtk struct as a void* */
58 	protected override void* getStruct()
59 	{
60 		return cast(void*)soupSocket;
61 	}
62 
63 	/**
64 	 * Sets our main struct and passes it to the parent class.
65 	 */
66 	public this (SoupSocket* soupSocket, bool ownedRef = false)
67 	{
68 		this.soupSocket = soupSocket;
69 		super(cast(GObject*)soupSocket, ownedRef);
70 	}
71 
72 	// add the Initable capabilities
73 	mixin InitableT!(SoupSocket);
74 
75 	/**
76 	 * Like soup_socket_read(), but reads no further than the first
77 	 * occurrence of @boundary. (If the boundary is found, it will be
78 	 * included in the returned data, and *@got_boundary will be set to
79 	 * %TRUE.) Any data after the boundary will returned in future reads.
80 	 *
81 	 * soup_socket_read_until() will almost always return fewer than @len
82 	 * bytes: if the boundary is found, then it will only return the bytes
83 	 * up until the end of the boundary, and if the boundary is not found,
84 	 * then it will leave the last <literal>(boundary_len - 1)</literal>
85 	 * bytes in its internal buffer, in case they form the start of the
86 	 * boundary string. Thus, @len normally needs to be at least 1 byte
87 	 * longer than @boundary_len if you want to make any progress at all.
88 	 *
89 	 * Params:
90 	 *     buffer = buffer to read
91 	 *         into
92 	 *     boundary = boundary to read until
93 	 *     boundaryLen = length of @boundary in bytes
94 	 *     nread = on return, the number of bytes read into @buffer
95 	 *     gotBoundary = on return, whether or not the data in @buffer
96 	 *         ends with the boundary string
97 	 *     cancellable = a #GCancellable, or %NULL
98 	 *
99 	 * Returns: as for soup_socket_read()
100 	 *
101 	 * Throws: GException on failure.
102 	 */
103 	public SoupSocketIOStatus readUntil(ubyte[] buffer, void* boundary, size_t boundaryLen, out size_t nread, bool* gotBoundary, Cancellable cancellable) {
104 		GError* err = null;
105 
106 		auto __p = soup_socket_read_until(soupSocket, buffer.ptr, cast(size_t)buffer.length, boundary, boundaryLen, &nread, cast(int *)gotBoundary, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
107 
108 		if (err !is null) {
109 			throw new GException( new ErrorG(err) );
110 	}
111 
112 	return __p;
113 }
114 
115 /**
116  */
117 
118 /** */
119 public static GType getType()
120 {
121 	return soup_socket_get_type();
122 }
123 
124 /**
125  * Begins asynchronously connecting to @sock's remote address. The
126  * socket will call @callback when it succeeds or fails (but not
127  * before returning from this function).
128  *
129  * If @cancellable is non-%NULL, it can be used to cancel the
130  * connection. @callback will still be invoked in this case, with a
131  * status of %SOUP_STATUS_CANCELLED.
132  *
133  * Params:
134  *     cancellable = a #GCancellable, or %NULL
135  *     callback = callback to call after connecting
136  *     userData = data to pass to @callback
137  */
138 public void connectAsync(Cancellable cancellable, SoupSocketCallback callback, void* userData)
139 {
140 	soup_socket_connect_async(soupSocket, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
141 }
142 
143 /**
144  * Attempt to synchronously connect @sock to its remote address.
145  *
146  * If @cancellable is non-%NULL, it can be used to cancel the
147  * connection, in which case soup_socket_connect_sync() will return
148  * %SOUP_STATUS_CANCELLED.
149  *
150  * Params:
151  *     cancellable = a #GCancellable, or %NULL
152  *
153  * Returns: a success or failure code.
154  */
155 public uint connectSync(Cancellable cancellable)
156 {
157 	return soup_socket_connect_sync(soupSocket, (cancellable is null) ? null : cancellable.getCancellableStruct());
158 }
159 
160 /**
161  * Disconnects @sock. Any further read or write attempts on it will
162  * fail.
163  */
164 public void disconnect()
165 {
166 	soup_socket_disconnect(soupSocket);
167 }
168 
169 /**
170  * Gets @sock's underlying file descriptor.
171  *
172  * Note that fiddling with the file descriptor may break the
173  * #SoupSocket.
174  *
175  * Returns: @sock's file descriptor.
176  */
177 public int getFd()
178 {
179 	return soup_socket_get_fd(soupSocket);
180 }
181 
182 /**
183  * Returns the #SoupAddress corresponding to the local end of @sock.
184  *
185  * Calling this method on an unconnected socket is considered to be
186  * an error, and produces undefined results.
187  *
188  * Returns: the #SoupAddress
189  */
190 public Address getLocalAddress()
191 {
192 	auto __p = soup_socket_get_local_address(soupSocket);
193 
194 	if(__p is null)
195 	{
196 		return null;
197 	}
198 
199 	return ObjectG.getDObject!(Address)(cast(SoupAddress*) __p);
200 }
201 
202 /**
203  * Returns the #SoupAddress corresponding to the remote end of @sock.
204  *
205  * Calling this method on an unconnected socket is considered to be
206  * an error, and produces undefined results.
207  *
208  * Returns: the #SoupAddress
209  */
210 public Address getRemoteAddress()
211 {
212 	auto __p = soup_socket_get_remote_address(soupSocket);
213 
214 	if(__p is null)
215 	{
216 		return null;
217 	}
218 
219 	return ObjectG.getDObject!(Address)(cast(SoupAddress*) __p);
220 }
221 
222 /**
223  * Tests if @sock is connected to another host
224  *
225  * Returns: %TRUE or %FALSE.
226  */
227 public bool isConnected()
228 {
229 	return soup_socket_is_connected(soupSocket) != 0;
230 }
231 
232 /**
233  * Tests if @sock is doing (or has attempted to do) SSL.
234  *
235  * Returns: %TRUE if @sock has SSL credentials set
236  */
237 public bool isSsl()
238 {
239 	return soup_socket_is_ssl(soupSocket) != 0;
240 }
241 
242 /**
243  * Makes @sock start listening on its local address. When connections
244  * come in, @sock will emit #SoupSocket::new_connection.
245  *
246  * Returns: whether or not @sock is now listening.
247  */
248 public bool listen()
249 {
250 	return soup_socket_listen(soupSocket) != 0;
251 }
252 
253 /**
254  * Attempts to read up to @len bytes from @sock into @buffer. If some
255  * data is successfully read, soup_socket_read() will return
256  * %SOUP_SOCKET_OK, and *@nread will contain the number of bytes
257  * actually read (which may be less than @len).
258  *
259  * If @sock is non-blocking, and no data is available, the return
260  * value will be %SOUP_SOCKET_WOULD_BLOCK. In this case, the caller
261  * can connect to the #SoupSocket::readable signal to know when there
262  * is more data to read. (NB: You MUST read all available data off the
263  * socket first. #SoupSocket::readable is only emitted after
264  * soup_socket_read() returns %SOUP_SOCKET_WOULD_BLOCK, and it is only
265  * emitted once. See the documentation for #SoupSocket:non-blocking.)
266  *
267  * Params:
268  *     buffer = buffer to read
269  *         into
270  *     nread = on return, the number of bytes read into @buffer
271  *     cancellable = a #GCancellable, or %NULL
272  *
273  * Returns: a #SoupSocketIOStatus, as described above (or
274  *     %SOUP_SOCKET_EOF if the socket is no longer connected, or
275  *     %SOUP_SOCKET_ERROR on any other error, in which case @error will
276  *     also be set).
277  *
278  * Throws: GException on failure.
279  */
280 public SoupSocketIOStatus read(ubyte[] buffer, out size_t nread, Cancellable cancellable)
281 {
282 	GError* err = null;
283 
284 	auto __p = soup_socket_read(soupSocket, buffer.ptr, cast(size_t)buffer.length, &nread, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
285 
286 	if (err !is null)
287 	{
288 		throw new GException( new ErrorG(err) );
289 	}
290 
291 	return __p;
292 }
293 
294 /**
295  * Starts using SSL on @socket, expecting to find a host named
296  * @ssl_host.
297  *
298  * Params:
299  *     sslHost = hostname of the SSL server
300  *     cancellable = a #GCancellable
301  *
302  * Returns: success or failure
303  */
304 public bool startProxySsl(string sslHost, Cancellable cancellable)
305 {
306 	return soup_socket_start_proxy_ssl(soupSocket, Str.toStringz(sslHost), (cancellable is null) ? null : cancellable.getCancellableStruct()) != 0;
307 }
308 
309 /**
310  * Starts using SSL on @socket.
311  *
312  * Params:
313  *     cancellable = a #GCancellable
314  *
315  * Returns: success or failure
316  */
317 public bool startSsl(Cancellable cancellable)
318 {
319 	return soup_socket_start_ssl(soupSocket, (cancellable is null) ? null : cancellable.getCancellableStruct()) != 0;
320 }
321 
322 /**
323  * Attempts to write @len bytes from @buffer to @sock. If some data is
324  * successfully written, the return status will be %SOUP_SOCKET_OK,
325  * and *@nwrote will contain the number of bytes actually written
326  * (which may be less than @len).
327  *
328  * If @sock is non-blocking, and no data could be written right away,
329  * the return value will be %SOUP_SOCKET_WOULD_BLOCK. In this case,
330  * the caller can connect to the #SoupSocket::writable signal to know
331  * when more data can be written. (NB: #SoupSocket::writable is only
332  * emitted after soup_socket_write() returns %SOUP_SOCKET_WOULD_BLOCK,
333  * and it is only emitted once. See the documentation for
334  * #SoupSocket:non-blocking.)
335  *
336  * Params:
337  *     buffer = data to write
338  *     nwrote = on return, number of bytes written
339  *     cancellable = a #GCancellable, or %NULL
340  *
341  * Returns: a #SoupSocketIOStatus, as described above (or
342  *     %SOUP_SOCKET_EOF or %SOUP_SOCKET_ERROR. @error will be set if the
343  *     return value is %SOUP_SOCKET_ERROR.)
344  *
345  * Throws: GException on failure.
346  */
347 public SoupSocketIOStatus write(ubyte[] buffer, out size_t nwrote, Cancellable cancellable)
348 {
349 	GError* err = null;
350 
351 	auto __p = soup_socket_write(soupSocket, buffer.ptr, cast(size_t)buffer.length, &nwrote, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
352 
353 	if (err !is null)
354 	{
355 		throw new GException( new ErrorG(err) );
356 	}
357 
358 	return __p;
359 }
360 
361 /**
362  * Emitted when the socket is disconnected, for whatever
363  * reason.
364  */
365 gulong addOnDisconnected(void delegate(SSocket) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
366 {
367 	return Signals.connect(this, "disconnected", dlg, connectFlags ^ ConnectFlags.SWAPPED);
368 }
369 
370 /**
371  * Emitted when a network-related event occurs. See
372  * #GSocketClient::event for more details.
373  *
374  * Params:
375  *     event = the event that occurred
376  *     connection = the current connection state
377  *
378  * Since: 2.38
379  */
380 gulong addOnEvent(void delegate(GSocketClientEvent, IOStream, SSocket) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
381 {
382 	return Signals.connect(this, "event", dlg, connectFlags ^ ConnectFlags.SWAPPED);
383 }
384 
385 /**
386  * Emitted when a listening socket (set up with
387  * soup_socket_listen()) receives a new connection.
388  *
389  * You must ref the @new if you want to keep it; otherwise it
390  * will be destroyed after the signal is emitted.
391  *
392  * Params:
393  *     new_ = the new socket
394  */
395 gulong addOnNewConnection(void delegate(SSocket, SSocket) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
396 {
397 	return Signals.connect(this, "new-connection", dlg, connectFlags ^ ConnectFlags.SWAPPED);
398 }
399 
400 /**
401  * Emitted when an async socket is readable. See
402  * soup_socket_read(), soup_socket_read_until() and
403  * #SoupSocket:non-blocking.
404  */
405 gulong addOnReadable(void delegate(SSocket) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
406 {
407 	return Signals.connect(this, "readable", dlg, connectFlags ^ ConnectFlags.SWAPPED);
408 }
409 
410 /**
411  * Emitted when an async socket is writable. See
412  * soup_socket_write() and #SoupSocket:non-blocking.
413  */
414 gulong addOnWritable(void delegate(SSocket) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
415 {
416 	return Signals.connect(this, "writable", dlg, connectFlags ^ ConnectFlags.SWAPPED);
417 }
418 }